# Basado en siPYgames

import os,sys,pygame,time,  Button,  Utilities
from Utilities import *
from Button import *

global mbuttons
global buttonPlay
global buttonExit
global bgimg
mbuttons = pygame.sprite.RenderUpdates()

def background(screen):
    global bgimg
    bgimg= load_image("Menu.jpg")
    bgrect=bgimg.get_rect()
    screen.blit(bgimg,bgrect)
    pygame.display.flip()

def buttons(screen):
    global mbuttons,  buttonPlay,  buttonExit
    buttonPlay = Button("Play.jpg",  "PlaySelected.jpg",  210, 500,  "play", 300, 50)
    buttonExit = Button("Exit.jpg", "ExitSelected.jpg",   210, 565,  "exit", 300, 50)
    mbuttons.add(buttonPlay)
    mbuttons.add(buttonExit)
    
def button_update(screen):
    global mbuttons,  buttonPlay,  buttonExit,  bgimg
    mbuttons.clear(screen,  bgimg)
    mbuttons.update()
    mbuttons.draw(screen)
    screen.blit(screen, (0, 0))
    pygame.display.flip()

def display(screen):
    background(screen)
    buttons(screen)
    
def handle(screen):
    action=""
    buttons=len(mbuttons)-1
    button_update(screen)
    while 1:
        event=pygame.event.wait()
        if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key in [pygame.K_ESCAPE, pygame.K_q]):
            action="EXIT"
            return action
        if event.type == pygame.MOUSEBUTTONDOWN:
            x,  y = pygame.mouse.get_pos()
            for button in mbuttons:
                if button.rect.colliderect(Rect(x,  y,  1,  1)):
                    if button.text == "play":
                        action = "PLAY"
                        return action
                    else:
                        action = "EXIT"
                        return action
        if event.type == MOUSEMOTION:
            x,  y = pygame.mouse.get_pos()
            for button in mbuttons:
                if button.rect.colliderect(Rect(x,  y,  1,  1)):
                    if not button.selected:
                        button.updateImage()
                else:
                    if button.selected:
                        button.updateImage()
            button_update(screen)

